package com.chenfwind.utils;

import java.io.UnsupportedEncodingException;

public class ByteUtils {
	static final String ENCODING_UTF_8 = "UTF-8";
	static final String ENCODING_GBK = "GBK";
	static final byte[] DEFAULT_0 = toByteArray(0);

	public static int tol(byte[] br) {
		return tol(br, 4);
	}

	public static int tol(byte[] br, int len) {
		int is = 0;
		if ((br == null) || (br.length < len)) {
			return -1;
		}
		for (int i = 0; i < len; i++) {
			is += ((br[i] & 0xFF) << 8 * i);
		}

		return is;
	}

	public static byte[] toByteArray(int isource) {
		return toByteArray(isource, 4);
	}

	public static byte[] toByteArray(int isource, int len) {
		byte[] bl = new byte[len];
		for (int i = 0; i < len; i++) {
			bl[i] = (byte) (isource >> 8 * i & 0xFF);
		}
		return bl;
	}

	public static byte[] revert(byte[] a, byte[] b) {
		if (a == null) {
			a = new byte[0];
		}
		if (b == null) {
			b = new byte[0];
		}
		byte[] result = new byte[a.length + b.length];
		System.arraycopy(a, 0, result, 0, a.length);
		System.arraycopy(b, 0, result, a.length, b.length);
		return result;
	}

	public static String dealAlpha(String s, int length) {
		try {
			if ((s == null) || ("".equals(s))) {
				s = "";
				for (int j = 0; j < length; j++)
					s = new StringBuilder().append(s).append(" ").toString();
			} else {
				if (s.length() > length)
					return null;
				if (s.length() < length) {
					int rest = length - s.length();
					for (int i = 0; i < rest; i++) {
						s = s.concat(" ");
					}
					return s;
				}
				return s;
			}
		} catch (RuntimeException e) {
			e.printStackTrace();
		}
		return s;
	}

	public static byte[] byteDealAlpha(byte[] by, int length) {
		if (by == null) {
			by = new byte[0];
			return by;
		}

		if (length % 8 != 0) {
			int num = 8 - length % 8;
			byte[] a = new byte[num];
			for (int i = 0; i < num; i++) {
				a[i] = (byte) num;
			}
			by = revert(by, a);
		}

		return by;
	}

	public static byte[] getLenAndValue(String value) {
		return getLenAndValue(value, null);
	}

	public static byte[] getLenAndValue(String value, String encoding) {
		try {
			if (encoding == null) {
				encoding = "UTF-8";
			}
			if (value == null) {
				value = "";
			}
			byte[] valueByte = value.getBytes("UTF-8");
			byte[] valueByteAll = revert(toByteArray(valueByte.length), valueByte);

			return valueByteAll;
		} catch (UnsupportedEncodingException e) {
			e.printStackTrace();
		}
		return DEFAULT_0;
	}

	public static byte[] getLenAndValue(byte[] value) {
		if (value == null) {
			value = new byte[0];
			return value;
		}
		byte[] valueByteAll = revert(toByteArray(value.length), value);
		return valueByteAll;
	}

	public static int getPart(byte[] cipherText) {
		int part = 0;
		if (cipherText.length / 2048 >= 1) {
			if (cipherText.length % 2048 != 0)
				part = cipherText.length / 2048 + 1;
			else
				part = cipherText.length / 2048;
		} else {
			part = 1;
		}
		return part;
	}

	public static byte[] hexString2ByteArray(String hexStr) {
		if (hexStr == null)
			return null;
		if (hexStr.length() % 2 != 0) {
			return null;
		}
		byte[] data = new byte[hexStr.length() / 2];
		for (int i = 0; i < hexStr.length() / 2; i++) {
			char hc = hexStr.charAt(2 * i);
			char lc = hexStr.charAt(2 * i + 1);
			byte hb = hexChar2Byte(hc);
			byte lb = hexChar2Byte(lc);
			if ((hb < 0) || (lb < 0)) {
				return null;
			}
			int n = hb << 4;
			data[i] = (byte) (n + lb);
		}
		return data;
	}

	public static byte hexChar2Byte(char c) {
		if ((c >= '0') && (c <= '9'))
			return (byte) (c - '0');
		if ((c >= 'a') && (c <= 'f'))
			return (byte) (c - 'a' + 10);
		if ((c >= 'A') && (c <= 'F'))
			return (byte) (c - 'A' + 10);
		return -1;
	}

	public static String byteArray2String(byte[] arr) {
		StringBuilder sbd = new StringBuilder();
		for (byte b : arr) {
			String tmp = Integer.toHexString(0xFF & b);
			if (tmp.length() < 2)
				tmp = new StringBuilder().append("0").append(tmp).toString();
			sbd.append(tmp);
		}
		return sbd.toString();
	}

	public static byte[] utf8Byte(String str) {
		try {
			return str.getBytes("UTF-8");
		} catch (UnsupportedEncodingException e) {
			e.printStackTrace();
		}
		return null;
	}

	public static String utf8String(byte[] plainBodyByte) {
		try {
			return new String(plainBodyByte, "UTF-8");
		} catch (UnsupportedEncodingException e) {
			e.printStackTrace();
		}
		return null;
	}
}